package net.xju_17_18.mapper;

import net.xju_17_18.entity.Card;
import net.xju_17_18.entity.Clazz;
import net.xju_17_18.entity.Stu;
import org.apache.ibatis.annotations.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public interface StuMapper {

    // 添加
//    @Insert("insert into newtable(sn,name) values(#{sn},#{name})")
    Integer insert(Stu stu);

    // 查询
//    @Select("select * from newtable")
    List<Stu> selectAll();

    // 修改
    Integer updateById(Stu stu1);

    // 删除
    Integer deleteById(int i);

    List<Stu> selectBySn(String number);

    Stu selectByNameAndSn(@Param("sn") String sn, @Param("name") String name);

    Stu selectByMap(HashMap<String, Object> map);

    @MapKey("id")
    Map<String,Object> selectBySn1(String sn);

    @Select("select n.id,n.sn,n.name,n.clazz_id,n.card_id,c.balance,c.id cid from  javaee.newtable n join card c on n.card_id  = c.id  where n.id = #{id}")
    @Results({
            @Result(property = "id" ,column = "id" ,id = true),
            @Result(property = "sn" ,column = "sn"),
            @Result(property = "name" ,column = "name"),
            @Result(property = "cardId" ,column = "card_id"),
            @Result(property = "clazzId" ,column = "clazz_id"),
            @Result(property = "card.id" ,column = "cid"),
            @Result(property = "card.balance" ,column = "balance"),
    })
//    @Results({
//            @Result(property = "id" ,column = "id" ,id = true),
//            @Result(property = "sn" ,column = "sn"),
//            @Result(property = "name" ,column = "name"),
//            @Result(property = "cardId" ,column = "card_id"),
//            @Result(property = "clazzId" ,column = "clazz_id"),
//            @Result(property = "card",
//                    column = "card_id",
//                    javaType = Card.class,
//                    one=@One(select = "net.xju.mapper.StuMapper.selectCardByCid")),
//    })
    Stu selectById(int id);

    @Select("select * from card where id = #{cid}")
    Card selectCardByCid(int cid);


    @Select(" select * from  javaee.clazz  where id = #{id}")
    @Results({
            @Result(property = "id",column = "id",id = true),
            @Result(property = "clazzName",column = "clazz_name"),
            @Result(property = "sum",column = "sum"),
            @Result(property = "students",
            column = "id",
            javaType = List.class,
            many = @Many(select = "net.xju.mapper.StuMapper.selectByCid"))
    })
    Clazz selectByClazzId(int id);

    @Select("select * from newtable where clazz_id = #{id}")
    List<Stu> selectByCid(Integer id);

    List<Stu> selectByIds(@Param("ids") List<Integer> ids);

    Integer batchInsert(List<Stu> stuList);
}
